package bb.lanxing.activity.discover;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.network.MyHttpClient;
import bb.lanxing.network.NetSubscribe;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class BbsReportCommitActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bbs_report_commit);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        TextView contentText = findViewById(R.id.content);
        TextView type = findViewById(R.id.type);

        String content = getIntent().getStringExtra("content");
        contentText.setText(content);
        String typeName = getIntent().getStringExtra("typeName");
        type.setText(typeName);
        int blogId = getIntent().getIntExtra("blogId", 0);
        int commentId = getIntent().getIntExtra("commentId", 0);
        int typeId = getIntent().getIntExtra("typeId", 0);

        TextView commitBtn = findViewById(R.id.commitBtn);
        commitBtn.setOnClickListener(v -> {
            commitReport(typeId, blogId, commentId, content);
        });
    }

    private void commitReport(int typeId, int blogId, int commentId, String content) {
        Observable.create(new NetSubscribe(MyHttpClient.commitReport(typeId, blogId, commentId, content))).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable th) {
                        toast("举报失败");
                    }

                    @Override
                    public void onNext(String str) {
                        System.out.println("commitReport: " + str);
                        if (!TextUtils.isEmpty(str)) {
                            try {
                                JSONObject jsonObject = new JSONObject(str);
                                int code = jsonObject.optInt("code");
                                if (code == 200) {
                                    toast("举报成功");
                                    finish();
                                }
                            } catch (JSONException e) {
                                throw new RuntimeException(e);
                            }
                        } else {
                            toast("举报失败");
                        }
                    }
                });
    }
}